
jQuery(document).ready(function () {


    window.onload = function () {

        addPageImageFunction();
        
        //var dropDownChoice = jQuery('select[id$="DropDownChoice"]');
        //if (dropDownChoice !== null) {
        //    var selectedVal = jQuery('select[id$="DropDownChoice"] option:selected');
        //    var textVal = selectedVal.text();
        //    if (textVal === "Microsoft People") {
        //        //hide control
        //    }
        //}
    };

    function addPageImageFunction() {
        var arUploadLinkText = {};
        arUploadLinkText[1036] = "Cliquez ici pour ajouter une image à partir de votre ordinateur.";
        arUploadLinkText[1033] = "Click here to insert a picture from your computer.";
        //For each RichImageFieldControl in the page (only in edit mode)

        var addAssetPromptLinkId = jQuery('a[id$="AddAssetPromptLink"]');
        if (addAssetPromptLinkId !== null) {
            jQuery('span.ms-metadata').hide();
            jQuery('a[id$="AddAssetPromptLink"]').each(function () {

                var selectorId = jQuery(this).attr("SelectorID");

                //Gets the translated text from the array
                var lkText = arUploadLinkText[SP.Res.lcid] ? arUploadLinkText[SP.Res.lcid] : arUploadLinkText[1033];

                //Adds a new link that allows to insert an image from the computer 
                var lkId = jQuery(this).attr("id") + "_FromComp";
                jQuery(this).after('<br /><a href="#" class="ms-toolbar ms-selectorlink" id="' + lkId + '" >' + lkText + '</a>');

                var $lk = jQuery("#" + lkId);
                $lk.click(function () {

                    //If the link is clicked, show the existing RteUploadDialog.aspx page as a popup
                    var dialog = {
                        url: SP.Utilities.Utility.getLayoutsPageUrl("RteUploadDialog.aspx") + "?LCID=" + SP.Res.lcid + "&Dialog=UploadImage&UseDivDialog=true&Source=" + encodeURIComponent(window.location),
                        allowMaximize: true,
                        showClose: true,
                        width: 500,
                        height: 160,
                        dialogReturnValueCallback: function (success, html) {
                            if (success) {
                                //Gets the picture's url from the popup, shows it and hides the links
                                var $emptyPanel = jQuery("#" + selectorId + "_EmptyPanel");
                                var $assetSelectedPanel = jQuery("#" + selectorId + "_AssetSelectedPanel");
                                var $imageFieldDisplay = jQuery("#" + selectorId + "_ImageFieldDisplay");

                                $imageFieldDisplay.html(html);
                                $emptyPanel.css("display", "none");
                                $assetSelectedPanel.css("display", "inline");
                            }
                        }
                    };

                    thisDialog = SP.UI.ModalDialog.showModalDialog(dialog);

                });

            });
        }
    }
});

//SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function () {

//    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({

//        OnPreRender: function (ctx) {
//            console.log('PreRender event is triggered');
//        },


//        OnPostRender: function (ctx) {
//            console.log('PostRender event is triggered');
//        }

//    });

//});